import request from '../../utils/request'
import { removeToken, setToken } from '@/utils/storage'

import { DEL_TOKEN, DEL_USERINFO, SET_TOKEN } from '../actionTypes'
export const Loginer = ({ mobile, code }) => {
  console.log('Loginer action', mobile, code)
  return async (dispatch) => {
    //  1.  发送ajax
    const res = await request({
      url: '/authorizations',
      method: 'post',
      data: { mobile, code }
    })
    console.log(res.data.token)
    // 2. 保存token到redux
    dispatch({ type: SET_TOKEN, payload: res.data.token })
    // 3. token持久化 存到本地
    setToken(res.data.token)
  }
}

// 退出功能
export const logout = () => {
  return (dispatch) => {
    // 1. 清空token
    removeToken()
    // 2. 清空redux
    dispatch({ type: DEL_TOKEN })
    dispatch({ type: DEL_USERINFO })
  }
}
